package com.huluobo.a2107kotlinshopping.category

import android.content.Intent
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.a2107kotlinshopping.R
import com.huluobo.a2107kotlinshopping.base.BaseMVVMFragment
import com.huluobo.a2107kotlinshopping.databinding.FragmentCategoryBinding
import com.huluobo.a2107kotlinshopping.detail.DetailActivity
import com.huluobo.a2107kotlinshopping.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding>() {
    private lateinit var viewModel: CategoryViewModel

    //一级分类的adapter
    private lateinit var parentAdapter: CategoryParentAdapter

    //一级分类的数据源
    private val categoryParentList = mutableListOf<CategoryBean>()

    //二级分类的adapter
    private lateinit var childAdapter: CategoryChildAdapter

    //二级分类的数据源
    private val categoryChildList = mutableListOf<CategoryBean>()

    //三级分类的adapter
    private lateinit var goodsListAdapter: CategoryGoodsListAdapter

    //三级分类的数据源
    private val goodsList = mutableListOf<CategoryGoodsBean>()

    private var currentPage = 1

    //刷新时需要使用的三级id
    private var goodsListId = 0
    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[CategoryViewModel::class.java]

        viewModel.getCategory()

        //一二级列表的数据变化
        viewModel.success.observe(this) {
            categoryParentList.clear()
            categoryParentList.addAll(it.data)
            parentAdapter.notifyDataSetChanged()

            //进入页面初始化时,需要同时加载二级列表中的数据
            val data = categoryParentList[0].data
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()

                //初始化二级列表的同时,需要初始化三级列表
                //同时需要拿到二级列表中第0位的三级列表id
                goodsListId = categoryChildList[0].id
                viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
            }
        }

        //三级列表的数据变化
        viewModel.getGoodsListSuccess.observe(this) {
            if (it.code == 200) {
                if (currentPage == 1) {
                    goodsList.clear()
                }
                goodsList.addAll(it.data)
                goodsListAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {

        //初始化一级列表
        dataBinding.categoryRvParent.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(categoryParentList)
        dataBinding.categoryRvParent.adapter = parentAdapter

        //一级列表的item点击事件
        parentAdapter.setOnItemClickListener { adapter, view, position ->
            val data = categoryParentList[position].data
            parentAdapter.setSelectItemPosition(categoryParentList[position].id)
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()

                if (categoryChildList.size != 0) {
                    val goodsListId = categoryChildList[0].id
                    viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
                }
            }
        }

        //初始化二级列表
        dataBinding.categoryRvChild.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(categoryChildList)
        dataBinding.categoryRvChild.adapter = childAdapter

        //二级列表的点击事件
        childAdapter.setOnItemClickListener { adapter, view, position ->
            if (categoryChildList.size != 0) {
                viewModel.getCategoryGoodsList(categoryChildList[position].id, currentPage, 10)
                goodsListId = categoryChildList[position].id
            }
        }

        //初始化三级列表
        dataBinding.categoryRvGoodsList.layoutManager = LinearLayoutManager(requireContext())
        goodsListAdapter = CategoryGoodsListAdapter(goodsList)
        dataBinding.categoryRvGoodsList.adapter = goodsListAdapter

        goodsListAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(requireContext(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }

        //初始化刷新框架
        dataBinding.categorySrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }

                currentPage = 1
                viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage += 1
                viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
            }
        })
    }
}